/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import excepciones.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import negocio.Tarifa;

/**
 *
 * @author Diego
 */
public class TarifaJpaController implements Serializable  {
    
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;
    
    public TarifaJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    public TarifaJpaController() {
        emf = Persistence.createEntityManagerFactory("entradasJpaPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public void create(Tarifa tarifa) throws PreexistingEntityException{
    EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(tarifa);
            em.getTransaction().commit();
        }
        catch(Exception ex){
            if(findTarifa(tarifa.getId()) != null){
                throw new PreexistingEntityException("Tarifa Existente",ex);
            }
            throw ex;
        }
        finally{
            if(em != null){
                em.close();
            }
        }    
    }
    public void destroy(Integer id){
        
    }
    public void edit (Tarifa tarifa){
        
    }
    public Tarifa findTarifa(int id){
        EntityManager em = getEntityManager();
        try {
            return em.find(Tarifa.class, id);
        } finally {
            em.close();
        }
    }
    
}
